<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 15/08/2017
 * Time: 18:33
 */
namespace common\models;

class TraderDao
{
    /**
     * 获取期权用户信息
     *
     * @param $uidArr
     * @return array
     */
    public static function moneyInfo($uidArr)
    {
        $db = \Yii::$app->db;

        // $gatherInfo = OrderDao::positionTradeInfo($uidArr);
        // var_dump($gatherInfo);
        // exit();
        $sql = "SELECT 
                    `userID`, 
                    `totalMoney`
                FROM 
                    `user_money` 
                WHERE 
                    `userID` IN (" . implode(',', $uidArr) . ")
         ";

        $tmp = $db->createCommand($sql)->queryAll();
        $return = [];
        foreach ($tmp as $v) {
            if (!isset($gatherInfo[$v['userID']])) {
                $gatherInfo[$v['userID']] = [
                    't_count' => 0,
                    't_tradeVolume' => 0,
                    't_deposit' => 0,
                ];
            }

            $moneyInfo = [
                'money' => $v['totalMoney'],//资金结余
            ];

            $return[$v['userID']] = array_merge($gatherInfo[$v['userID']], $moneyInfo);
        }

        return $return;
    }


    /**
     * 期权手续费返现
     *
     * @param array $session 发起查询的代理信息
     * @param int $pid 该代理的某个下级代理
     * @param int $pLevel 下级代理的级别
     * @param int $uid 该代理下面的终端客户
     * @param int $start 起始时间
     * @param int $end 结束时间
     * @param int $pageNo
     * @param int $pageSize
     *
     * @return array
     */
    public static function tradeCostReturn(
        $session,
        $pid = -1,
        $pLevel = -1,
        $uid = -1,
        $start = -1,
        $end = -1,
        $pageNo = 1,
        $pageSize = 15
    ) {
        $table = "order_history";
        if ($session['is_last']) {
            $tmpField = "a.`trade_cost_my`";
        } else {
            $tmpField = "a.`level_" . $session['level_cid'] . "_trade_cost`";
        }
        $field = "($tmpField - a.`level_" . $session['level'] . "_trade_cost`) * a.`volume`";

        $sql_count = "SELECT COUNT(1) count, SUM($field) sumReturn, SUM(`volume`) sumVolume FROM `" . $table . "` a ";
        $sql_lists = "
            SELECT 
                a.`volume`,
                $tmpField trade_cost_sub,
                a.`level_" . $session['level'] . "_trade_cost` trade_cost_my,
                $field trade_cost_return, 
                a.`level_" . $session['level'] . "_return_flag` return_flag,
                a.`time`,
                a.`create_time`,
                b.`uid`,
                b.`clientSN`,
                b.`account`,
                b.`alias`
            FROM  
                `" . $table . "` a 
        ";
        $sql_join = " LEFT JOIN `user_trade` b ON a.`uid` = b.`uid` ";
        $sql_where = "";
        $sql_orderBy = " ORDER BY a.`create_time` DESC ";
        $sql_limit = " LIMIT " . ($pageNo - 1) * $pageSize . "," . $pageSize;

        $bind = [];
        if ($start != -1) {
            $tmpWhere[] = "a.`create_time` >= :time_start";
            $bind[':time_start'] = $start;
        }
        if ($end != -1) {
            $tmpWhere[] = "a.`create_time` <= :time_end";
            $bind[':time_end'] = $end;
        }
        $tmpWhere[] = "a.`level_" . $session['level'] . "_uid` = :uidSession";
        $bind[':uidSession'] = $session['uid'];
        if ($pid != -1) {
            $tmpWhere[] = "a.`level_" . $pLevel . "_uid` = :pid";
            $bind[':pid'] = $pid;
        }
        if ($uid != -1) {
            $tmpWhere[] = "a.`uid` = :uid";
            $bind[':uid'] = $uid;
        }
        if (!empty($tmpWhere)) {
            $sql_where .= " WHERE " . implode(' AND ', $tmpWhere);
        }

        $tmp = \Yii::$app->db->createCommand($sql_count . $sql_join . $sql_where, $bind)->queryOne();
        $lists = \Yii::$app->db->createCommand(
            $sql_lists . $sql_join . $sql_where . $sql_orderBy . $sql_limit,
            $bind
        )->queryAll();

        $return = [
            'count' => $tmp ? $tmp['count'] : 0,
            'sumReturn' => $tmp['sumReturn'] ? $tmp['sumReturn'] : 0,
            'sumVolume' => $tmp['sumVolume'] ? $tmp['sumVolume'] : 0,
            'lists' => $lists
        ];

        return $return;
    }
}
